package com.db.httpserver.io;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.db.httpserver.foo.Parameter;

public final class IoWorkerThread implements Runnable {

	private static Socket socket;

	public IoWorkerThread(Socket socket) {
		this.socket = socket;
	}

	public static void startPool(int suggestedPort) throws IOException {
        Parameter.setPort(suggestedPort);
        Parameter.print();
        ExecutorService executor = Executors.newFixedThreadPool(Parameter.getWorkers());        
        ServerSocket serverSocket = new ServerSocket(Parameter.getPort());
        Socket socket;
        while(true) {
        	socket = serverSocket.accept();
        	socket.setSoTimeout(Parameter.getTimeout());
        	executor.execute(new IoWorkerThread(socket));
        }
    }

    public void run() {
    	try {
			new IoWorker().handleRequest(socket);
		} catch (IOException e) {
			System.out.println("OOOps");
		}
    	
    }
}